<?php

/**
 * SignupController is the default controller for this application
 */
class SignupController extends BrowseControllerBase {
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'signup' );
	}
	
	public function indexAction() {
		$actionForm = $this->getPost ( 'actionForm', '' );
		$firstName = $this->getPost ( 'firstName', '' );
		$lastName = $this->getPost ( 'lastName', '' );
		
		$month = $this->getPost ( 'month', '' );
		$day = $this->getPost ( 'day', '' );
		$year = $this->getPost ( 'year', '' );
		if ($year != '' && $month != '' && $day != '') {
			$birthday = $year . "-" . $month . "-" . $day;
		} else
			$birthday = '';
		
		$country = $this->getPost ( 'country', '' );
		$city = $this->getPost ( 'city', '' );
		$address = $this->getPost ( 'address', '' );
		$telephone = $this->getPost ( 'telephone', '' );
		$mobile = $this->getPost ( 'mobile', '' );
		$fax = $this->getPost ( 'fax', '' );
		$email = $this->getPost ( 'email', '' );
		$username = $this->getPost ( 'username', '' );
		$password = $this->getPost ( 'password', '' );
		$customerType = $this->getPost ( 'customerType', '' );
		$authenticate = new Authentication ();
		
		//check submit
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			$usersDataService = new UsersDataService ();
			if ($usersDataService->checkExistEmail ( $email ) != 1) {
				try {
					$usersEntity = $usersDataService->beforeCreate ();
					$usersEntity->username = $username;
					$usersEntity->email = $email;
					$usersEntity->password = $authenticate->encryptPassword ( $password );
					$usersEntity->title = "";
					$usersEntity->firstName = $firstName;
					$usersEntity->lastName = $lastName;
					$usersEntity->birthday = $birthday;
					$usersEntity->countryId = $country;
					$usersEntity->city = $city;
					$usersEntity->address = $address;
					$usersEntity->telephone = $telephone;
					$usersEntity->mobile = $mobile;
					$usersEntity->fax = $fax;
					
					$usersEntity->userType = ROLE_MEMBER;
					$usersEntity->status = UsersEntity::INACTIVE;
					$usersEntity->lastVisit = "";
					
					// Not yet Activated Member
					$usersEntity->hasVerified = 0;
					$usersEntity->activationCode = md5 ( $username . "vinatay" );
					$userId = $usersDataService->create ( $usersEntity );
					$resultMessage = 'SignUp succeeded';
					
					//send thank mail and pass activation code here
					$usersDataService->sendThankMailAndActivate ( $usersEntity, $password );
					
					// SEND MAIL TO ADMINISTRATOR
					$mailer = new MyMailUtils ();
					$mailer->addFrom ( ADMIN_EMAIL, ADMIN_NAME );
					$mailer->addTo ( ADMIN_EMAIL, ADMIN_NAME );
					/*
					 $message = "
					 Dear ". ADMIN_NAME . ", <br>
					 A new account ({$usersEntity->email}) has been successfully created.<br>
					 ";*/
					$mailReplace = array (ADMIN_NAME, $usersEntity->email );
					$contentEmail = SystemEmailDataService::getEmailContent ( SYSTEM_EMAIL_SIGNUP_NOTIFICATION_TO_ADMIN, $mailReplace );
					$subject = $contentEmail ['subject'];
					$message = $contentEmail ['message'];
					$mailer->send ( $subject, $message );
					$mailer->resetAll ();
					
					$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Login/thank.tpl' );
					$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
					return true;
				
				} catch ( Exception $ex ) {
					
					$this->smarty->assign ( 'username', $username );
					$this->smarty->assign ( 'firstName', $firstName );
					$this->smarty->assign ( 'lastName', $lastName );
					$this->smarty->assign ( 'month', $month );
					$this->smarty->assign ( 'day', $day );
					$this->smarty->assign ( 'year', $year );
					$this->smarty->assign ( 'country', $country );
					$this->smarty->assign ( 'city', $city );
					$this->smarty->assign ( 'address', $address );
					$this->smarty->assign ( 'telephone', $telephone );
					$this->smarty->assign ( 'mobile', $mobile );
					$this->smarty->assign ( 'fax', $fax );
					$this->smarty->assign ( 'email', $email );
					$this->smarty->assign ( 'password', $password );
					$this->smarty->assign ( 'isError', true );
					$this->smarty->assign ( 'errMsg', "database" );
					$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/SignUp/index.tpl' );
					$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
					return true;
				}
				//$url = Zend_Registry::get('config')->APPLICATION_URL . '/Browse/SignUp';
				//CommonUtils::redirect($url);
				$this->smarty->assign ( 'resultMessage', $resultMessage );
			} else {
				
				$messageUser = 'existing users. Please check';
				$this->smarty->assign ( 'messageUser', $messageUser );
				$this->smarty->assign ( 'firstName', $firstName );
				$this->smarty->assign ( 'lastName', $lastName );
				$this->smarty->assign ( 'month', $month );
				$this->smarty->assign ( 'day', $day );
				$this->smarty->assign ( 'year', $year );
				$this->smarty->assign ( 'country', $country );
				$this->smarty->assign ( 'city', $city );
				$this->smarty->assign ( 'address', $address );
				$this->smarty->assign ( 'telephone', $telephone );
				$this->smarty->assign ( 'mobile', $mobile );
				$this->smarty->assign ( 'fax', $fax );
				$this->smarty->assign ( 'email', $email );
				$this->smarty->assign ( 'password', $password );
				$this->smarty->assign ( 'isError', true );
				$this->smarty->assign ( 'errMsg', "existedEmail" );
				$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/SignUp/index.tpl' );
				$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
				return true;
			}
		}
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/SignUp/index.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	/**
	 * Create capcha for imagesecurity
	 *
	 */
	public function createsecurityAction() {
		$width = $this->getQuery ( 'width', 120 );
		$height = $this->getQuery ( 'height', 40 );
		$characters = $this->getQuery ( 'characters', 6 );
		$captcha = new CaptchaSecurityImages ( $width, $height, $characters );
	}
	
	/**
	 * Signup Action handling
	 */
	public function asignupAction() {
		$result = ERROR;
		$message = 'Signup Fails. Please try again!';
		
		$email = $this->getPost ( 'email', '' );
		$username = $this->getPost ( 'username', '' );
		$password = $this->getPost ( 'password', '' );
		$confirmPassword = $this->getPost ( 'confirmPassword', '' );
		$firstName = $this->getPost ( 'firstName', '' );
		$lastName = $this->getPost ( 'lastName', '' );
		$brandFree = $this->getPost ( 'brandFree', '0' );
		$actionForm = $this->getPost ( 'actionForm', '' );
		$packageOptionId = $this->getPost ( 'packageOptionId', '' );
		
		if ($password != $confirmPassword) {
			$result = ERROR;
			$message = 'Password mismatch! Please try again!';
			
			$rturn = array ("error" => $result, "message" => $message );
			
			echo json_encode ( $rturn );
			exit ();
		}
		
		try {
			$authenticate = new Authentication ();
			if ($actionForm == "submit" && $username != "" && $password != "" && $packageOptionId != "") {
				$usersDataService = new UsersDataService ();
				$usersEntity = $usersDataService->beforeCreate ();
				$usersEntity->username = $username;
				$usersEntity->email = $email;
				$usersEntity->password = $authenticate->encryptPassword ( $password );
				$usersEntity->title = "";
				$usersEntity->firstName = $firstName;
				$usersEntity->lastName = $lastName;
				$usersEntity->userType = ROLE_MEMBER;
				$usersEntity->status = UsersEntity::ACTIVE;
				$usersEntity->brandFree = $brandFree;
				$usersEntity->lastVisit = "";
				$usersEntity->paymentMethod = "";
				// Not yet Activated Member
				$usersEntity->hasVerified = 0;
				$usersEntity->packageOptionId = $packageOptionId;
				
				// Set Default TemplateID
				$demoUser = $usersDataService->getByUserName ( DEMO_USERNAME );
				if ($demoUser) {
					$usersEntity->mainTemplateId = $demoUser->mainTemplateId;
				}
				
				$result = 1;
				$message = "";
				if ($usersDataService->checkExistUsernameAndOwnerId ( $username, 0 ) == 1) {
					$result = 0;
					$message = '$username is exist in system ! ';
				
				}
				if ($usersDataService->checkExistEmailAndOwnerId ( $email, 0 ) == 1) {
					$result = 0;
					$message .= '$email is exist in system !';
				
				}
				if ($result != 0) {
					$usersEntity->ownerId = 0;
					$usersEntity->activationCode = md5 ( $username . "vinatay" );
					$userId = $usersDataService->create ( $usersEntity );
					
					// Try to login this user
					$result = $usersDataService->login ( $username, $password );
					if ($result != SUCC) {
						$result = "User login fails!";
					} else {
						$result = SUCC;
						$message = "Create User successfully";
					}
				}
			}
		} catch ( Exception $ex ) {
			$message = $ex->getMessage ();
		}
		
		$rturn = array ("error" => $result, "message" => $message );
		
		echo Zend_Json::encode ( $rturn );
		exit ();
	}
	
	/**
	 * 
	 */
	private function setDemoTemplate(UsersEntity $usersEntity, UsersDataService $usersDataService) {
	
	}

}
